package com.bsj.media.tool;

import com.alibaba.fastjson.JSON;
import com.bsj.media.init.NettyLiveUDPIntercomUntil;
import com.bsj.media.init.SendCachedData;
import com.bsj.media.init.SendChannelUntil;
import com.bsj.media.network.websocket.CmdDto;
import com.bsj.power.common.config.cached.RedisCached;
import com.bsj.power.common.config.redis.RedisConstant;
import com.bsj.power.common.def.dto.media.RealCtrolParam;
import com.bsj.power.common.def.dto.media.RealVideoParam;
import com.bsj.power.common.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.Map;

/**
 * @author yinhao
 * @version 1.0
 * @description 直播控制方法
 * @date 2024/8/5
 */
@Slf4j
public class HttpPlayVideoTool {

    static VideoCmdUntil videoCmdUntil;
    static RedisCached redisCached;

    public static void init() {
        videoCmdUntil = (VideoCmdUntil) SpringUtil.getBean("videoCmdUntil");
        redisCached = SpringUtil.getBean(RedisCached.class);
    }


    /**
     * 直播的开启与关闭
     * @param cmdDto
     * @param terminalNo
     * @param channelNo
     */
    public static void cmdLiveMedia(CmdDto cmdDto, String terminalNo, int channelNo) {
        //0是播放，1是关闭
        int videoFlag = cmdDto.getVideoFlag();
        String str = cmdDto.getStr();
        String simChannel = cmdDto.getSimChannel();
        if (videoFlag == 0) {
            //打开直播
            String channelStr = redisCached.hget(RedisConstant.VIDEO_PLAY_STATUS, simChannel);
            String resolutionRatio = cmdDto.getResolutionRatio();
            if (resolutionRatio != null && channelStr != null) {
                //判断是切换分辨率,则需要发送直播关闭指令，然后再发直播打开指令
                liveCloseCmd(terminalNo, channelNo, false);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.info("视频直播码率切换: {}, 待切换码率值： {}", JSON.toJSONString(cmdDto), resolutionRatio);
                liveMediaCmd(terminalNo, channelNo, resolutionRatio, str);
                return;
            }
            if (channelStr == null) {
                //下发相关的http操作
                liveMediaCmd(terminalNo, channelNo, null, str);
            } else {
                SendCachedData sendCachedData = SendChannelUntil.gainObj(terminalNo + "-" + channelNo);
                if (sendCachedData == null) {
                    //在这之前没有人观看，则需要先关闭，再下发开启直播指令
                    liveCloseCmd(terminalNo, channelNo, true);
                    //开启
                    liveMediaCmd(terminalNo, channelNo, null, str);
                } else {
                    log.info("直播是打开的，不再下发直播开启指令: {}", cmdDto.getSimChannel());
                }
            }
        } else {
            //关闭直播
            int i = SendChannelUntil.closeMediaFlag(simChannel);
            if (i < 1) {
                liveCloseCmd(terminalNo, channelNo, true);
            }
        }
    }

    /**
     * 直播指令开启
     * @param terminalNo
     * @param channelNo
     * @param str
     */
    public static void liveMediaCmd(String terminalNo, int channelNo, String resolutionRatio, String str) {
        RealVideoParam realVideoParam = new RealVideoParam();
        realVideoParam.setDataType(0);
        realVideoParam.setTerminalNo(terminalNo);
        realVideoParam.setChannelNo(channelNo);
        log.info("发送直播开启指令： {} ", realVideoParam.toString());
        videoCmdUntil.realVideoReq(realVideoParam, channelNo, resolutionRatio);
    }

    /**
     * 直播关闭
     * @param terminalNo
     * @param channelNo
     * @param ratio true 删除通道  false 不删除通道
     */
    public static void liveCloseCmd(String terminalNo, int channelNo, boolean ratio) {
        //直播
        RealCtrolParam controlVO = new RealCtrolParam();
        controlVO.setChannelNo(channelNo);
        controlVO.setCloseType(0);
        controlVO.setControlType(0);
        controlVO.setTerminalNo(terminalNo);
        String simChannel = terminalNo + "-" + channelNo;
        if (ratio) {
            SendChannelUntil.del(simChannel);
        }
        videoCmdUntil.realControlReq(controlVO);
        //todo
//        NettyLiveUDPIntercomUntil.del(terminalNo);
    }
}
